package com.meiyu.live.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.meiyu.live.R;
import com.meiyu.live.util.ToastUtil;

import java.util.Calendar;


/**
 * 邀约主播对话框
 */
public class StartEndTimeDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private DatePicker datePickerStart, datePickerEnd;
    private View btn_ok, btn_cancel, btn_start_time, btn_end_time;
    private ImageView iv_underline_start, iv_underline_end;

    private DatePickerDialogListener datePickerDialogListener;

    private Calendar calendarStart = Calendar.getInstance(), calendarEnd = Calendar.getInstance();

    public interface DatePickerDialogListener {
        void onSelectTime(Calendar start, Calendar end);
    }

    public StartEndTimeDialog(Context context) {
        super(context, R.style.dialog_custom);
        this.context = context;
        init(context);
    }

    public StartEndTimeDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        setContentView(R.layout.dialog_startend_time);
        setCanceledOnTouchOutside(false);

        datePickerStart = (DatePicker) findViewById(R.id.datepicker_start);
        datePickerStart.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, Calendar calendar, int year, int monthOfYear, int dayOfMonth, int hour, int minute) {
                calendarStart = calendar;
            }
        });
        datePickerEnd = (DatePicker) findViewById(R.id.datepicker_end);
        datePickerEnd.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, Calendar calendar, int year, int monthOfYear, int dayOfMonth, int hour, int minute) {
                calendarEnd = calendar;
            }
        });

        btn_ok = findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(this);
        btn_cancel = findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(this);
        btn_start_time = findViewById(R.id.btn_start_time);
        btn_start_time.setOnClickListener(this);
        btn_end_time = findViewById(R.id.btn_end_time);
        btn_end_time.setOnClickListener(this);
        iv_underline_start = (ImageView) findViewById(R.id.iv_underline_start);
        iv_underline_end = (ImageView) findViewById(R.id.iv_underline_end);

        setState(0);
    }

    /**
     * 0开始时间，1结束时间
     *
     * @param state
     */
    private void setState(int state) {
        if (state == 0) {
            datePickerStart.setVisibility(View.VISIBLE);
            datePickerEnd.setVisibility(View.GONE);
            iv_underline_start.setVisibility(View.INVISIBLE);
            iv_underline_end.setVisibility(View.VISIBLE);
        } else {
            datePickerStart.setVisibility(View.GONE);
            datePickerEnd.setVisibility(View.VISIBLE);
            iv_underline_end.setVisibility(View.INVISIBLE);
            iv_underline_start.setVisibility(View.VISIBLE);
        }
    }

    public void setDatePickerDialogListener(DatePickerDialogListener datePickerDialogListener) {
        this.datePickerDialogListener = datePickerDialogListener;
    }

    @Override
    public void onClick(View view) {
        if (view == btn_cancel) {
            dismiss();
        } else if (view == btn_ok) {
            if (calendarEnd.compareTo(calendarStart) <= 0) {
                ToastUtil.show("结束时间必须在开始时间之后");
                return;
            }
            if (datePickerDialogListener != null) {
                datePickerDialogListener.onSelectTime(calendarStart, calendarEnd);
                dismiss();
            }
        } else if (view == btn_start_time) {
            setState(0);
        } else if (view == btn_end_time) {
            setState(1);
        }
    }
}
